package com.mobi.cacti.rabbit.controllers;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;

import android.content.Context;
import android.util.Log;

import com.mobi.cacti.rabbit.connectors.AbstractServerConnector;
import com.mobi.cacti.rabbit.connectors.ServerConnectorCallback;
import com.mobi.cacti.rabbit.connectors.TicketDetailServerConnector;
import com.mobi.cacti.rabbit.connectors.TicketListServerConnector;
import com.mobi.cacti.rabbit.response.Cache;
import com.mobi.cacti.rabbit.response.ReqTicketDetail;
import com.mobi.cacti.rabbit.response.ReqTicketHistory;
import com.mobi.cacti.rabbit.utils.SAXXMLParser;

public class TicketDetailActivityController implements ServerConnectorCallback {
	
	private TicketDetailServerConnector ticetDetailServerConnector;
	
	public TicketDetailActivityControllerCallback delegate;
	private static TicketDetailActivityController instance = null;

	public static TicketDetailActivityController getInstance() {
		if(instance == null) {
			instance = new TicketDetailActivityController();
		}
		return instance;
	}
	
	public void loadTicketDetailForTicketId(Context context, String ticketID)
	{
		if(!Cache.isNetworkAvailable(context))
		{
			Cache.showNoInternetAlertDialog(context);
			return ;
		}
		
		ticetDetailServerConnector = new TicketDetailServerConnector();
		ticetDetailServerConnector.delegate = this;
		ticetDetailServerConnector.loadTicketDetailForTicket(ticketID);
	}

	@Override
	public void serverConnectorFinishBackgroundTask(
			AbstractServerConnector serverConnector, Object response) {
		
		String requestTicketID = ((String[]) response)[0];
		String responseData = ((String[]) response)[1];
		
//		Log.i("tai", "Ticket detail = " + requestTicketID + " >>> " + responseData);
		
		InputStream is = new ByteArrayInputStream(responseData.getBytes());
		ReqTicketDetail ticketDetail = SAXXMLParser.respTicketDetail(is);
		
//		Log.i("tai", "Ticket detail = " + ticketDetail.faultTypeCode);
		
		this.delegate.ticketDetailActivityControllerDidLoadTicketDetail(ticketDetail);
		
	}

}
